package com.topcent.tioxyc.config;

import com.topcent.tioxyc.constants.RabbitConsts;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitConfig {

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);             //开启手动 ack
        return factory;
    }
    // 声明队列
    @Bean
    public Queue queueA() {
        return new Queue(RabbitConsts.QUEUE_A, true);
    }

    @Bean
    public Queue imgProcess() {
        return new Queue(RabbitConsts.IMGPROCESS, true);
    }
    @Bean
    public Queue costOrderAvg() {
        return new Queue(RabbitConsts.COSTORDERAVG, true);
    }

    @Bean
    public Queue feiYongAddCost() {
        return new Queue(RabbitConsts.FEIYONGADDCOST, true);
    }

    @Bean
    public Queue queueB() {
        return new Queue(RabbitConsts.QUEUE_B, true);
    }

    @Bean
    public Queue feiyong() {
        return new Queue(RabbitConsts.FEI_YONG, true);
    }
    @Bean
    public Queue SalesReceiptTracking() {
        return new Queue(RabbitConsts.SALESRECEIPTTRACKING, true);
    }

    @Bean
    public Queue PaymentTracking() {
        return new Queue(RabbitConsts.PAYMENTTRACKING, true);
    }

    @Bean
    public Queue skdTracking() {
        return new Queue(RabbitConsts.SKDTracking, true);
    }

    @Bean
    public Queue FuKuanDanNew() {
        return new Queue(RabbitConsts.FuKuanDanNew, true);
    }
    @Bean
    public Queue stockquantity(){
        return new Queue(RabbitConsts.STOCK_QUANTITY, true);
    }
    @Bean
    public Queue PurchaseRequest(){
        return new Queue(RabbitConsts.PurchaseRequest, true);
    }
    @Bean
    public Queue PaymentsController(){
        return new Queue(RabbitConsts.PAYMENTS, true);
    }

    // 声明交换机
    @Bean
    public TopicExchange exchangeA() {
        return new TopicExchange(RabbitConsts.EXCHANGE_A);
    }

    @Bean
    public FanoutExchange exchangeB() {
        return new FanoutExchange(RabbitConsts.EXCHANGE_B);
    }



    // 配置RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

//    // 配置消息监听容器
//    @Bean
//    public SimpleMessageListenerContainer listenerContainerA(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapterA) {
//        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
//        container.setConnectionFactory(connectionFactory);
//        container.setQueueNames(RabbitConsts.QUEUE_A);
//        container.setMessageListener(listenerAdapterA);
//        return container;
//    }
//
//    @Bean
//    public SimpleMessageListenerContainer listenerContainerB(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapterB) {
//        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
//        container.setConnectionFactory(connectionFactory);
//        container.setQueueNames(RabbitConsts.QUEUE_B);
//        container.setMessageListener(listenerAdapterB);
//        return container;
//    }
//
//    @Bean
//    public SimpleMessageListenerContainer listenerContainerC(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapterC) {
//        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
//        container.setConnectionFactory(connectionFactory);
//        container.setQueueNames(RabbitConsts.QUEUE_C);
//        container.setMessageListener(listenerAdapterC);
//        return container;
//    }
//
//    // 配置消息监听适配器
//    @Bean
//    public MessageListenerAdapter listenerAdapterA(YourMessageHandlerA handlerA) {
//        return new MessageListenerAdapter(handlerA, "handleMessage");
//    }
}
